﻿Imports TransportadoraToledoDAL
Imports System.Data

Public Class DestinatarioBLL
    Dim objDAL As DestinatarioDAL
#Region "Propriedades"

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property

    Private logradouro As String
    Public Property pLogradouro() As String
        Get
            Return logradouro
        End Get
        Set(ByVal value As String)
            logradouro = value
        End Set
    End Property

    Private complemento As String
    Public Property pComplemento() As String
        Get
            Return complemento
        End Get
        Set(ByVal value As String)
            complemento = value
        End Set
    End Property

    Private numero As String
    Public Property pNumero() As String
        Get
            Return numero
        End Get
        Set(ByVal value As String)
            numero = value
        End Set
    End Property

    Private bairro As String
    Public Property pBairro() As String
        Get
            Return bairro
        End Get
        Set(ByVal value As String)
            bairro = value
        End Set
    End Property

    Private cep As String
    Public Property pCEP() As String
        Get
            Return cep
        End Get
        Set(ByVal value As String)
            cep = value
        End Set
    End Property

    Private nome As String
    Public Property pNome() As String
        Get
            Return nome
        End Get
        Set(ByVal value As String)
            nome = value
        End Set
    End Property

#End Region

#Region "Construtores"

    Sub New()

    End Sub

    Sub New(codigo As Integer, logradouro As String, complemento As String, numero As String, bairro As String,
            cep As String, nome As String)
        Me.codigo = codigo
        Me.logradouro = logradouro
        Me.complemento = complemento
        Me.numero = numero
        Me.bairro = bairro
        Me.cep = cep
        Me.nome = nome
    End Sub

#End Region

#Region "Métodos"

    Sub inserir()
        Try
            If (nome = "") Then
                Throw New Exception("Preencha o campo nome")
            Else
                'inserir
                objDAL = New DestinatarioDAL
                Me.codigo = objDAL.inserir(nome, complemento, numero, bairro, cep, nome)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Try
            If (codigo = 0) Then
                Throw New Exception("O código não pode ser 0")
            ElseIf (nome = "") Then
                Throw New Exception("Preencha o campo nome")
            Else
                'alterar
                objDAL = New DestinatarioDAL
                objDAL.alterar(codigo, nome, complemento, numero, bairro, cep, nome)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Try
            If (codigo = 0) Then
                Throw New Exception("O código não pode ser 0")
            Else
                'excluir
                objDAL = New DestinatarioDAL
                objDAL.excluir(codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New DestinatarioDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("des_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("des_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New DestinatarioDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("des_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("des_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    'retorna todas as linhas pesquisadas da tabela
    Function localizar(ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Try

            objDAL = New DestinatarioDAL
            tab = objDAL.localizar(nome)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    'localizar uma linha da tabela e preenche as propriedades
    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New DestinatarioDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("des_codigo").ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function



    'localizar uma linha da tabela e preenche as propriedades
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New DestinatarioDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("des_codigo").ToString
                Me.logradouro = tab.Rows(0)("des_complemento").ToString
                Me.complemento = tab.Rows(0)("seg_valor").ToString
                Me.numero = tab.Rows(0)("des_numero").ToString
                Me.bairro = tab.Rows(0)("des_bairro").ToString
                Me.cep = tab.Rows(0)("des_cep").ToString
                Me.nome = tab.Rows(0)("des_nome").ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub limpar()
        Me.codigo = Nothing
        Me.logradouro = Nothing
        Me.complemento = Nothing
        Me.numero = Nothing
        Me.bairro = Nothing
        Me.cep = Nothing
        Me.nome = Nothing
    End Sub
#End Region

End Class
